package org.alino.core.shiro.filter;

import org.alino.core.shiro.session.AlinoSessionManager;
import org.alino.core.shiro.session.SessionStatus;
import org.alino.core.utils.*;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Desc TODO
 * @Author by Brant
 * @Date 2017/1/13
 */
public class SimpleAuthFilter extends AccessControlFilter {
    private static final String KICKED_OUT_URL = "kickout";
    private static final String LOCK_OUT_URL = "lock";
    private String kickedOutUrl;
    private String lockUrl;
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        HttpServletRequest httpRequest = ((HttpServletRequest)request);
        String url = httpRequest.getRequestURI();
        if(url.startsWith("/open/")){
            return Boolean.TRUE;
        }
        Subject subject = getSubject(request, response);
        Session session = subject.getSession();
        SessionStatus sessionStatus = (SessionStatus) session.getAttribute(AlinoSessionManager.SESSION_STATUS);
        if (null != sessionStatus && (sessionStatus.getOnlineStatus()==SessionStatus.SESSION_STATUS_LOGOUT || sessionStatus.getOnlineStatus()==SessionStatus.SESSION_STATUS_LOCK)) {
            if (RequestUtils.isAjaxRequest((HttpServletRequest) request) ) {
                LoggerUtils.debug(getClass(), sessionStatus.getOnlineStatus()==SessionStatus.SESSION_STATUS_LOGOUT?"当前用户已经被踢出，Ajax请求！":"当前用户已经锁定，Ajax请求！");
                JsonUtils.write((HttpServletResponse) response, sessionStatus.getOnlineStatus()==SessionStatus.SESSION_STATUS_LOGOUT? ReturnUtils.KICKOUT_LOGIN:ReturnUtils.LOCK_LOGIN);
            }
            return  Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        //先退出
        Subject subject = getSubject(request, response);
        Session session = subject.getSession();
        SessionStatus sessionStatus = (SessionStatus) session.getAttribute(AlinoSessionManager.SESSION_STATUS);
        if(sessionStatus.getOnlineStatus() == SessionStatus.SESSION_STATUS_LOGOUT){
            subject.logout();
        }
        WebUtils.saveRequest(request);
        WebUtils.issueRedirect(request, response, sessionStatus.getOnlineStatus() == SessionStatus.SESSION_STATUS_LOGOUT?(StringUtils.isNotBlank(getKickedOutUrl())?getKickedOutUrl():KICKED_OUT_URL):(StringUtils.isNotBlank(getLockUrl())?getLockUrl():LOCK_OUT_URL));
        return false;
    }

    public String getKickedOutUrl() {
        return kickedOutUrl;
    }

    public void setKickedOutUrl(String kickedOutUrl) {
        this.kickedOutUrl = kickedOutUrl;
    }

    public String getLockUrl() {
        return lockUrl;
    }

    public void setLockUrl(String lockUrl) {
        this.lockUrl = lockUrl;
    }
}
